package main

import (
	"context"
	"fmt"
	"gin_blog/cron"
	"gin_blog/models"
	"gin_blog/pkg/gredis"
	"gin_blog/pkg/logging"
	"gin_blog/pkg/setting"
	"gin_blog/routers"
	"log"
	"net/http"
	"os"
	"os/signal"
	"time"
)


// @title gin_blog
// @version 1.0
// @description 一个好的 API's，必然离不开一个好的API文档,要开发纯手写 API 文档，不存在的 :=)
//// @termsOfService [http://swagger.io/](http://swagger.io/)`
//// @contact.name huangzhe`
////@contact.url [http://www.swagger.io/support](http://www.swagger.io/support)`
////@contact.email support@swagger.io`
////@license.name Apache 2.0`
////@license.url [http://www.apache.org/licenses/LICENSE-2.0.html](http://www.apache.org/licenses/LICENSE-2.0.html)`
// @securityDefinitions.apikey ApiKeyAuth
// @in header
// @name Authorization
// @host localhost:8000
// @BasePath

func init() {
	setting.Setup()
	models.Setup()
	err := gredis.Setup()
	if err!=nil{
		logging.Error(err)
	}
	logging.Setup()
}

func main()  {
	router :=routers.InitRouter()


	s :=&http.Server{
		Addr: fmt.Sprintf(":%d",setting.ServerSetting.HttpPort),
		Handler: router,
		ReadTimeout: setting.ServerSetting.ReadTimeout,
		WriteTimeout: setting.ServerSetting.WriteTimeout,
		MaxHeaderBytes: 1 << 20,
	}
	fmt.Printf("Listening and serving HTTP on %v\n", setting.ServerSetting.HttpPort)

	go func() {
		if err :=s.ListenAndServe();err!=nil{
			logging.Info(err)
		}
	}()
	go cron.Cron()
	// Wait for interrupt signal to gracefully shutdown the server with
	// a timeout of 5 seconds.
	//优雅重启或停止 Shutdown
	quit := make(chan os.Signal)
	signal.Notify(quit, os.Interrupt)
	<- quit

	log.Println("Shutdown Server ...")

	ctx, cancel := context.WithTimeout(context.Background(), 5 * time.Second)
	defer cancel()
	if err := s.Shutdown(ctx); err != nil {
		log.Fatal("Server Shutdown:", err)
	}

	log.Println("Server exiting")
}
